﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Utils
{
    public static class EnumerableExtensions
    {
        public static string ToCommaDelimitedList(this IEnumerable list)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ", ";
                }

                text += item;
            }

            return text;
        }

        public static string ToCommaDelimitedList(this IEnumerable list, Func<string, string> postProcess)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ",";
                }

                text += postProcess(item);
            }

            return text;
        }

        public static string ToCommaDelimitedList(this IEnumerable list, Func<string, bool, string> postProcess)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();
            var isLast = false;

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ",";
                }
                else
                {
                    isLast = true;
                }

                text += postProcess(item, isLast);
            }

            return text;
        }

        public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
        {
            var index = 0;

            foreach (var item in source)
            {
                if (predicate(item))
                {
                    return index;
                }

                index++;
            }

            return -1;
        }

        // This provides a useful extension-like method to find the index of and item from IEnumerable<T>
        // This was based off of the Enumerable.Count<T> extension method.
        /// <summary>
        /// Returns the index of an item in a sequence.
        /// </summary>
        /// <typeparam name="T">The type of the elements of source.</typeparam>
        /// <param name="source">A sequence containing elements.</param>
        /// <param name="item">The item to locate.</param>        
        /// <returns>The index of the entry if it was found in the sequence; otherwise, -1.</returns>
        public static int IndexOf<TSource>(this IEnumerable<TSource> source, TSource item)
        {
            return IndexOf<TSource>(source, item, null);
        }

        // This provides a useful extension-like method to find the index of and item from IEnumerable<T>
        // This was based off of the Enumerable.Count<T> extension method.
        /// <summary>
        /// Returns the index of an item in a sequence.
        /// </summary>
        /// <typeparam name="T">The type of the elements of source.</typeparam>
        /// <param name="source">A sequence containing elements.</param>
        /// <param name="item">The item to locate.</param>
        /// <param name="itemComparer">The item equality comparer to use.  Pass null to use the default comparer.</param>
        /// <returns>The index of the entry if it was found in the sequence; otherwise, -1.</returns></returns>
        public static int IndexOf<TSource>(this IEnumerable<TSource> source, TSource item, IEqualityComparer<TSource> itemComparer)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            IList<TSource> listOfT = source as IList<TSource>;
            if (listOfT != null)
            {
                return listOfT.IndexOf(item);
            }

            IList list = source as IList;
            if (list != null)
            {
                return list.IndexOf(item);
            }

            int i = 0;
            foreach (TSource possibleItem in source)
            {
                if (itemComparer.Equals(item, possibleItem))
                {
                    return i;
                }
                i++;
            }
            return -1;
        }
    }
}
